package com.sztx.pay.center.tool;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.sztx.commons.httpclient.HttpClientConfigBuilder;
import com.sztx.commons.httpclient.HttpClientUtil;
import com.sztx.commons.httpclient.HttpHeaderBuilder;

public class AbcEmerchantAddUser {
	private static final Logger logger = LoggerFactory.getLogger(AbcEmerchantAddUser.class);	

	public static String PUBLISH_URL = "http://cashier.berbon.com/abc/user/addUser?format=json";
		
	private static List<String> failList = new ArrayList<String>();
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new FileReader("E:/20170329/adduser4.txt"));
		
		String readLine = null;
		int i = 0;
		int s = 0;
		int m = 500;
		while ((readLine = bf.readLine()) != null) {// 使用readLine方法，一次读一行	
			if(++i < s){
				continue;
			}
			if(i > m){
				break;
			}
			try {
				TimeUnit.MILLISECONDS.sleep(200l);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			readLine = readLine.trim();
			
			//logger.info("line:{}",readLine);
			
			String[] splitArr = readLine.split("\\|");
			System.out.println(splitArr.length);
			if(splitArr.length != 7){
				failList.add(readLine);
				continue;
			}
			String payerNo = splitArr[0];
			String userCode = splitArr[1];
			String userName = splitArr[2];
			
			Map<String,String> parasMap = new HashMap<String, String>();
			parasMap.put("userName", userName);
			parasMap.put("userId", userCode);
			parasMap.put("status", "1");
			parasMap.put("payerNo", payerNo);
			
			String result = getHtmlContent(PUBLISH_URL, parasMap);
			try {
				JSONObject parseObject = JSONObject.parseObject(result);
				String code = (String)parseObject.get("code");
				String description = (String)parseObject.get("description");
				if(code.equals("J000000")){
					
				}else{
					failList.add("payerno:"+payerNo+"   userId:"+userCode+"  userName:"+userName+" 添加失败,description:"+description);
					logger.info("payerno:{}   userId:{}   userName:{}  添加失败,description:{}",payerNo,userCode,userName,description);
				}
			} catch (Exception e) {
				logger.error("",e);
			}
			
		}
		
		for (String str : failList) {
			if(!str.contains("无需重复添加")){
				logger.info("failAddUserINfo--------"+str);
			}
		}
		
		
		logger.info("failListSize:{},failList:{}",failList.size(),JSONObject.toJSONString(failList));
		
		
	}
	
	
	private static String getHtmlContent(String url,Map<String,String> parasMap) throws IOException{	
		HttpClientConfigBuilder clientBuilder = HttpClientConfigBuilder.custom();
		HttpHeaderBuilder headerBuilder = HttpHeaderBuilder.custom().cookie("berbonjsessionid=B6C2B70961F6455EB9A5D1D7B79EC22D");
		String encoding = "UTF-8";
		return HttpClientUtil.post(clientBuilder, url, parasMap, headerBuilder, encoding);
	}
	
	
	
}
